package frame.PayDetail;

import MyBatis.MyBatislianjian;
import frame.GuanliPanel;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.GoodMapper;
import org.example.mapper.PayDetailMapper;
import org.example.mapper.PayMainMapper;
import org.example.pojo.Good;
import org.example.pojo.PayDetail;
import org.example.pojo.PayMain;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class PayDetailselect extends JInternalFrame {
    private JTextField[] inputFields = new JTextField[2]; // 两个输入框
    private JLabel[] inputLabels = new JLabel[2]; // 输入框说明标签数组
    private JComboBox<String> gidComboBox;
    private JComboBox<Integer> pidComboBox;
    private JTable resultTable; // 显示查询结果的表格
    private DefaultTableModel tableModel;
    private SqlSession sqlSession = MyBatislianjian.getSqlSession();
    private PayDetailMapper payDetailMapper = sqlSession.getMapper(PayDetailMapper.class);
    private GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
    private PayMainMapper payMainMapper = sqlSession.getMapper(PayMainMapper.class);

    public PayDetailselect() {
        setTitle("查询采购明细");
        setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建顶部的查询输入区域
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 10, 5, 10); // 设置组件间距

        // 商品编号
        inputLabels[0] = new JLabel("商品编号: ");
        inputLabels[0].setFont(new Font("微软雅黑", Font.PLAIN, 16));
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(inputLabels[0], gbc);

        gidComboBox = new JComboBox<>();
        gidComboBox.addItem(""); // 添加空白选项
        populateGidComboBox(); // 填充商品编号下拉框
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        inputPanel.add(gidComboBox, gbc);

        // 采购清单号
        inputLabels[1] = new JLabel("采购清单号: ");
        inputLabels[1].setFont(new Font("微软雅黑", Font.PLAIN, 16));
        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(inputLabels[1], gbc);

        pidComboBox = new JComboBox<>();
        pidComboBox.addItem(null); // 添加空白选项
        populatePidComboBox(); // 填充采购清单号下拉框
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        inputPanel.add(pidComboBox, gbc);

        add(inputPanel, BorderLayout.NORTH);

        // 创建表格模型
        String[] columnNames = {"采购明细ID", "采购清单ID", "商品编号", "采购数量", "商品单价", "商品总价", "备注"};
        tableModel = new DefaultTableModel(columnNames, 0);
        resultTable = new JTable(tableModel);
        resultTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        resultTable.setRowHeight(25);

        // 添加表格到滚动面板
        JScrollPane scrollPane = new JScrollPane(resultTable);
        add(scrollPane, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton searchButton = new JButton("搜索");
        searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performSearch();
            }
        });
        buttonPanel.add(searchButton);

        JButton returnButton = new JButton("返回");
        returnButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GuanliPanel.setContent(new PayDetailPanel());
            }
        });
        buttonPanel.add(returnButton);

        add(buttonPanel, BorderLayout.SOUTH);

        // 设置窗口大小并显示
        pack();
        setVisible(true);
    }

    // 执行搜索的方法
    private void performSearch() {
        String selectedGid = (String) gidComboBox.getSelectedItem();
        Integer selectedPid = (Integer) pidComboBox.getSelectedItem();

        // 构造查询条件
        PayDetail payDetailQuery = new PayDetail();
        payDetailQuery.setGid(selectedGid);
        payDetailQuery.setPid(selectedPid);

        List<PayDetail> payDetails = payDetailMapper.queryPayDetail(payDetailQuery);

        if (payDetails.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有符合条件的采购明细信息！", "提示", JOptionPane.INFORMATION_MESSAGE);
        }

        updateTableData(payDetails);
    }

    // 更新表格数据的方法
    private void updateTableData(List<PayDetail> payDetails) {
        // 清空表格
        tableModel.setRowCount(0);

        // 添加新的数据
        for (PayDetail payDetail : payDetails) {
            Object[] row = {
                    payDetail.getPDid(), payDetail.getPid(), payDetail.getGid(), payDetail.getPcount(),
                    payDetail.getGpay(), payDetail.getTotal(), payDetail.getOther()
            };
            tableModel.addRow(row);
        }
    }

    // 填充商品编号下拉框
    private void populateGidComboBox() {
        List<Good> goods = goodMapper.selectAll();
        for (Good good : goods) {
            gidComboBox.addItem(good.getGid());
        }
    }

    // 填充采购清单号下拉框
    private void populatePidComboBox() {
        List<PayMain> payMains = payMainMapper.selectAll();
        for (PayMain payMain : payMains) {
            pidComboBox.addItem(payMain.getPid());
        }
    }
}
